package com.lzy.web;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzy.entity.IMTask;
import com.lzy.service.TaskService;
import com.lzy.util.Constants;
import com.lzy.util.Page;
import com.lzy.util.PageRequest;
import com.lzy.web.form.JsonResponse4Ext;

@Controller
@RequestMapping(value="/task")
public class TaskController {
	
	private TaskService taskService ;
	
	@Autowired
	public void setService(TaskService service) {
		this.taskService = service;
	}
	
	@RequestMapping(value="/index/{id}")
	public String index(@PathVariable int id, ModelMap modelMap){
		modelMap.addAttribute("id", id);
		if(id==1){
			return "task/index";
		}else{
			return "task/index1";
		}
	}
	
	@RequestMapping(value="/list/{id}")
	@ResponseBody
	public Page<IMTask> list(@PathVariable int id, ModelMap modelMap){
		
		PageRequest pageRequest = new PageRequest();
		
		Page<IMTask> imTasks = this.taskService.queryTaskList(pageRequest,id);
		
		modelMap.addAttribute("result",imTasks);
		
		return imTasks;
	}
	
	@RequestMapping(value="/add")
	@ResponseBody
	public JsonResponse4Ext add(@RequestParam Map<String, Object> params){
		JsonResponse4Ext response4Ext = new JsonResponse4Ext();
		try {
			this.taskService.addIMTask(params);
		} catch (Exception e) {
			e.printStackTrace();
			response4Ext.setErrorMessages(e.getMessage());
			response4Ext.setSuccess(false);
			return response4Ext;
		}
		
		return response4Ext;
	}
	
	@RequestMapping(value="/addPre")
	public String addPre(ModelMap modelMap ){
		modelMap.addAttribute("action", "add");
		return "task/taskForm";
	}
	
	@RequestMapping(value="/load/taskId/{taskId}")
	public String load(@PathVariable int taskId,ModelMap modelMap ){
		
		modelMap.addAttribute("result", this.taskService.queryIMTask(taskId));
		modelMap.addAttribute("action", "edit");
		return "task/taskForm";
	}
	
	@RequestMapping(value="/del")
	@ResponseBody
	public String del(@RequestParam Map<String,Object> params){

		String ids =(String)params.get("id");
		String[] strings = ids.split(",");
		int[] idins = new int[strings.length];
		for (int i = 0; i < strings.length; i++) {
			idins[i] = Integer.valueOf(strings[i]);
		}
		this.taskService.delIMTask(idins);
		
		return "success";
	}
	
	@RequestMapping(value="/start")
	@ResponseBody
	public String start(@RequestParam Map<String,Object> params){

		String ids =(String)params.get("id");
		String[] strings = ids.split(",");
		int[] idins = new int[strings.length];
		for (int i = 0; i < strings.length; i++) {
			idins[i] = Integer.valueOf(strings[i]);
		}
		this.taskService.startIMTask(idins);
		
		return "success";
	}
	
	@RequestMapping(value="/stop")
	@ResponseBody
	public String stop(@RequestParam Map params){

		String ids =(String)params.get("id");
		String[] strings = ids.split(",");
		int[] idins = new int[strings.length];
		for (int i = 0; i < strings.length; i++) {
			idins[i] = Integer.valueOf(strings[i]);
		}
		this.taskService.stopIMTask(idins);
		
		return "success";
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	@ResponseBody
	public JsonResponse4Ext update(@RequestParam Map params ){
		try {
			
			this.taskService.updateIMTask(params);
		} catch (Exception e) {
			return new JsonResponse4Ext(Constants.Status.failure.value());
		}
		return new JsonResponse4Ext(Constants.Status.success.value());
	}
	
}
